﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using BTL.OnlineTestingSocial.Domain.TestingEngine;
using BTL.Technology.Data;

#endregion

namespace BTL.OnlineTestingSocial.Services.TestingEngine.Service.DAL
{
    public class QuestionGroupRepository : IQuestionGroupRepository
    {
        private readonly IDataHelper _dataHelper;
        private readonly DbSetting _dbSettings;

        public QuestionGroupRepository(DbSetting dbSettings, IDataHelper dataHelper)
        {
            _dbSettings = dbSettings;
            _dataHelper = dataHelper;
        }

        #region IQuestionGroupRepository Members

        /// <summary>
        /// Get Question Groups list for only me mode
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageNum"></param>
        /// <returns></returns>
        public IEnumerable<QuestionGroupItem> GetPrivateQuestionGroups(int userId, int pageSize, int pageNum)
        {
            var res = _dataHelper.FillCollection<QuestionGroupItem>(
                _dbSettings.ConnectionString,
                _dbSettings.GetPrivateUserQuestionGroupsSp, userId, pageSize, pageNum);

            if (res.Count > 0)
            {
                var ids = "";

                foreach (var item in res)
                    ids += item.Id + ",";

                ids = ids.Substring(0, ids.Length - 1);

                var comts = _dataHelper.FillCollection<Comment>(
                    _dbSettings.ConnectionString,
                    _dbSettings.GetQuestionGroupCommentsSp, ids, "all");

                foreach (var ques in res)
                {
                    var tmp = from c in comts where c.QuestionGroupId == ques.Id select c;
                    ques.Comments = tmp.ToList();
                }
            }

            return res;
        }

        /// <summary>
        /// Get Question Groups list for public mode
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageNum"></param>
        /// <returns></returns>
        public IEnumerable<QuestionGroupItem> GetPublicQuestionGroups(int userId, int pageSize, int pageNum)
        {
            var res = _dataHelper.FillCollection<QuestionGroupItem>(
                _dbSettings.ConnectionString,
                _dbSettings.GetPublicUserQuestionGroupsSp, userId, pageSize, pageNum);

            if (res.Count > 0)
            {
                var ids = "";

                foreach (var item in res)
                    ids += item.Id + ",";

                ids = ids.Substring(0, ids.Length - 1);

                var top = "all";

                var comts = _dataHelper.FillCollection<Comment>(
                    _dbSettings.ConnectionString,
                    _dbSettings.GetQuestionGroupCommentsSp, ids, top);

                foreach (var ques in res)
                {
                    var tmp = from c in comts where c.QuestionGroupId == ques.Id select c;
                    ques.Comments = tmp.ToList();
                }
            }

            return res;
        }

        /// <summary>
        /// delete question group and detail, comment, like belong to it
        /// </summary>
        /// <param name="ids">question group id array</param>
        /// <returns></returns>
        public bool DeleteQuestionGroupSp(string ids)
        {
            try
            {
                _dataHelper.ExecuteNonQuery(
                    _dbSettings.ConnectionString,
                    _dbSettings.DeleteQuestionGroupSp, ids);
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// get question group detail and question list belong to
        /// </summary>
        /// <param name="questionGroupId"></param>
        /// <returns></returns>
        public QuestionGroupItem GetDetail(int questionGroupId)
        {
            try
            {
                var res = _dataHelper.FillObject<QuestionGroupItem>(
                _dbSettings.ConnectionString,
                _dbSettings.GetQuestionGroupDetailSp, questionGroupId);

                if (res != null)
                {
                    var top = "all";

                    var comts = _dataHelper.FillCollection<Comment>(
                        _dbSettings.ConnectionString,
                        _dbSettings.GetQuestionGroupCommentsSp, questionGroupId.ToString(), top);

                    res.Comments = comts;

                    var ques = _dataHelper.FillCollection<Question>(
                        _dbSettings.ConnectionString,
                        _dbSettings.GetQuestionsListSp, questionGroupId);

                    res.Questions = ques;

                    return res;
                }
            }
            catch (Exception)
            {
                throw;
            }

            return null;
        }

        #endregion
    }
}